package com.ehotting.mqbd.zw.netty.netty;

import com.ehotting.eaf.netty.client.TcpClient;
import com.ehotting.eaf.netty.loggers.NettyMsgReceiveLogger;
import com.ehotting.eaf.netty.loggers.NettyMsgSendLogger;
import com.ehotting.eaf.netty.sync.SyncFutureMapHelper;
import com.ehotting.mqbd.api.bean.solver.SolverInfoBean;
import com.ehotting.mqbd.core.manager.AgentLinkManager;
import com.ehotting.mqbd.core.manager.SolverStateManager;
import com.ehotting.mqbd.core.sync.TcpClientMapHelper;
import io.netty.bootstrap.Bootstrap;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class ZwTcpMultiClient {

    /**
     * 读操作空闲秒数
     **/
    @Value("${netty.readIdle}")
    private Integer readIdle;
    /**
     * 写操作空闲秒数
     **/
    @Value("${netty.writeIdle}")
    private Integer writeIdle;
    /**
     * 读写空闲秒数
     **/
    @Value("${netty.allIdle}")
    private Integer allIdle;

    @Autowired
    private ZwNettyActionFactory zwNettyActionFactory;

    @Resource(name = "zwNettySyncFutureMapHelper")
    private SyncFutureMapHelper zwNettySyncFutureMapHelper;
    @Autowired
    private SolverStateManager solverStateManager;
    @Autowired
    private AgentLinkManager agentLinkManager;
    @Autowired
    private NettyMsgReceiveLogger solverMsgLogger;
    @Autowired
    private NettyMsgSendLogger nettyMsgSendLogger;
    @Autowired
    private TcpClientMapHelper tcpClientMapHelper;




    public TcpClient getByAgentIdAndIp(String module,String agentId, SolverInfoBean solverInfo){
        TcpClient zwTcpClient = tcpClientMapHelper.get(agentId);

        if(zwTcpClient==null){
            return createTcpClient( module,agentId, solverInfo);
        }

        if( zwTcpClient.getChannel()==null ) {
            return createTcpClient( module,agentId, solverInfo);
        }

        if( !zwTcpClient.getChannel().isActive()) {
            agentLinkManager.deleteAgentIdByChannelId(module,zwTcpClient.getChannel().id().asLongText());
            return createTcpClient( module,agentId, solverInfo);
        }

        return zwTcpClient;
    }

    private ZwTcpClient createTcpClient(String module,String agentId,SolverInfoBean solverInfo){
        log.info("solverInfo.getIp():{},solverInfo.getPort():{}",solverInfo.getIp(),solverInfo.getPort());
        ZwTcpClient zwTcpClient = new ZwTcpClient();
        zwTcpClient.setIp(solverInfo.getIp());
        zwTcpClient.setPort(Integer.valueOf(solverInfo.getPort()));
        zwTcpClient.setBootstrap(new Bootstrap());

        ZwClientInitializer channelInitializer = new ZwClientInitializer();
        channelInitializer.setAllIdle(allIdle);
        channelInitializer.setReadIdle(readIdle);
        channelInitializer.setWriteIdle(writeIdle);

        ZwNettyHandler handler = new ZwNettyHandler();
        handler.setZwNettyActionFactory(zwNettyActionFactory);
        handler.setNettyMsgReceiveLogger(solverMsgLogger);
        handler.setZwNettySyncFutureMapHelper(zwNettySyncFutureMapHelper);
        handler.setAgentLinkManager(agentLinkManager);

        channelInitializer.setChannelInboundHandler(handler);

        zwTcpClient.setChannelInitializer(channelInitializer);
        zwTcpClient.setNettyMsgSendLogger(nettyMsgSendLogger);
        zwTcpClient.setZwNettySyncFutureMapHelper(zwNettySyncFutureMapHelper);
        zwTcpClient.setSolverStateManager(solverStateManager);

        handler.setTcpClient(zwTcpClient);

        try {
            zwTcpClient.serverStartSync();
            tcpClientMapHelper.put(agentId, zwTcpClient);
            agentLinkManager.setAgentIdByChannelId(module,zwTcpClient.getChannel().id().asLongText(),agentId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return zwTcpClient;
    }

}